import Vue from 'vue'
import Router from 'vue-router'

const Home = () => import('../views/home/Home');
const Category = () => import('../views/category/Category');
const Cart = () => import('../views/cart/Cart');
const Profile = () => import('../views/profile/Profile');

// 1.注入插件
Vue.use(Router);

// 2.定义路由
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    meta: {
      title: '首页'
    }
  },
  {
    path: '/category',
    component: Category,
    meta: {
      title: '分类'
    }
  },
  {
    path: '/cart',
    component: Cart,
    meta: {
      title: '购物车'
    }
  },
  {
    path: '/profile',
    component: Profile,
    meta: {
      title: '我的'
    }
  }
];

// 3.创建router对象
const router = new Router({
  routes: routes,
  mode: 'history'
});

router.beforeEach((to, from, next) => {
  window.document.title = to.matched[0].meta.title;
  next();
});

// 4.导出router对象
export default router;
